package cc.comac.mom.webservice.impl;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

import cc.comac.mom.entity.QualityQarCheckInvoiceEntity;
import cc.comac.mom.util.ResultBean;
import cc.comac.mom.util.TimeUtil;
import cc.comac.mom.webservice.QualityQarCheckInvoiceWebService;
// 提交限售件信息给限售件管理系统
public class QualityQarCheckInvoiceWebServiceImpl implements QualityQarCheckInvoiceWebService {

	@Override
	public ResultBean submitLimitLife(QualityQarCheckInvoiceEntity qualityQarCheckInvoiceEntity) throws Exception {
		// QAR编号
		String qarNo = qualityQarCheckInvoiceEntity.getQarNo();
		// 件号
		String partNo = qualityQarCheckInvoiceEntity.getPartNo();
		// 件号名称
		String partName = qualityQarCheckInvoiceEntity.getPartName();
		// 序列号
		String serialNumber = qualityQarCheckInvoiceEntity.getSerialNumber();
		// 批次号
		String stoveNo = qualityQarCheckInvoiceEntity.getStoveNo();
		// 所属成品件号
		String finishedProduct = qualityQarCheckInvoiceEntity.getFinishedProduct();
		// 所属成品件号名称
		String finishedProductName = qualityQarCheckInvoiceEntity.getFinishedProductName();
		// 成品序列号
		String finishedProductSn = qualityQarCheckInvoiceEntity.getFinishedProductSn();
		// 成品批次号
		String finishedProductBatch = qualityQarCheckInvoiceEntity.getFinishedProductBatch();
		// 库存期限
		String limiteDeadline = qualityQarCheckInvoiceEntity.getLimiteDeadline();
		// 基准日期
		Date baseDate = qualityQarCheckInvoiceEntity.getBaseDate();
		// 生产日期
		LocalDateTime creationTime = qualityQarCheckInvoiceEntity.getCreationTime();
		// 维护日期
		LocalDateTime lastModifiedTime = qualityQarCheckInvoiceEntity.getLastModifiedTime();
		// 供应商交货日期
		Date supplierDeliveryTime = qualityQarCheckInvoiceEntity.getSupplierDeliveryTime();
		// 检验人员
		String checkUser = qualityQarCheckInvoiceEntity.getCheckUser();
		// 检验时间
		Date checkDate = qualityQarCheckInvoiceEntity.getCheckDate();

		String myNamespace = "mom";
		String myNamespaceURI = "urn://www.comac.com/mom";

		// Create SOAP Connection
		SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();

		SOAPConnection soapConnection = soapConnectionFactory.createConnection();
		// Create SOAP Message
		MessageFactory messageFactory = MessageFactory.newInstance("SOAP 1.1 Protocol");
		SOAPMessage soapMessage = messageFactory.createMessage();
		MimeHeaders hd = soapMessage.getMimeHeaders();
		hd.addHeader("Authorization", "Basic UElURVNUOmNvbWFjMTIz");
		SOAPPart soapPart = soapMessage.getSOAPPart();

		// SOAP Envelope
		SOAPEnvelope envelope = soapPart.getEnvelope();
		envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);

		// SOAP Body
		SOAPBody soapBody = envelope.getBody();
		SOAPElement soapBodyElem = soapBody.addChildElement("MT_MOM_QM_Request_Out", myNamespace);

		SOAPElement requestElem = soapBodyElem.addChildElement("QualityInspectionResult");

		SOAPElement brNoEle = requestElem.addChildElement("qarNo");
		brNoEle.addTextNode(qarNo);
		SOAPElement contentEle = requestElem.addChildElement("partCde");
		contentEle.addTextNode(partNo);
		SOAPElement reqDate = requestElem.addChildElement("partName");
		reqDate.addTextNode(partName);
		SOAPElement reqSerialEle = requestElem.addChildElement("serialNum");
		reqSerialEle.addTextNode(serialNumber);
		SOAPElement batch = requestElem.addChildElement("batch");
		batch.addTextNode(stoveNo);
		SOAPElement reqTime = requestElem.addChildElement("endProductCode");
		reqTime.addTextNode(finishedProduct);
		SOAPElement token = requestElem.addChildElement("endProductName");
		token.addTextNode(finishedProductName);
		SOAPElement txCode = requestElem.addChildElement("endSerialNum");
		txCode.addTextNode(finishedProductSn);
		SOAPElement endBatch = requestElem.addChildElement("endBatch");
		endBatch.addTextNode(finishedProductBatch);
		SOAPElement aa = requestElem.addChildElement("inventoryDeadline");
		aa.addTextNode(limiteDeadline);
		SOAPElement bb = requestElem.addChildElement("baseDate");
		String fomatDate = TimeUtil.fomatDate(baseDate);
		bb.addTextNode(fomatDate);
		SOAPElement manufactureDate = requestElem.addChildElement("manufactureDate");
		Date fomatLocalDateTimeToDate2 = TimeUtil.fomatLocalDateTimeToDate(creationTime);
		String fomatDate1 = TimeUtil.fomatDate(fomatLocalDateTimeToDate2);
		manufactureDate.addTextNode(fomatDate1);
		SOAPElement maintainDate = requestElem.addChildElement("maintainDate");
		Date fomatLocalDateTimeToDate3 = TimeUtil.fomatLocalDateTimeToDate(lastModifiedTime);
		String fomatDate2 = TimeUtil.fomatDate(fomatLocalDateTimeToDate3);
		maintainDate.addTextNode(fomatDate2);
		SOAPElement deliveryDate = requestElem.addChildElement("deliveryDate");
		String fomatDate4 = TimeUtil.fomatDate(supplierDeliveryTime);
		deliveryDate.addTextNode(fomatDate4);
		SOAPElement inspect = requestElem.addChildElement("inspect");
		inspect.addTextNode(checkUser);
		SOAPElement inspectDate = requestElem.addChildElement("inspectDate");
		String fomatDate3 = TimeUtil.fomatDate(checkDate);
		inspectDate.addTextNode(fomatDate3);
		soapMessage.saveChanges();
		soapMessage.writeTo(System.out);
		// Send SOAP Message to SOAP Serve
		SOAPMessage soapResponse = soapConnection.call(soapMessage,
				"http://10.14.3.83/XISOAPAdapter/MessageServlet?senderParty=&senderService=BC_MOM&receiverParty=&receiverService=&interface=SI_MOM_QM_Out&interfaceNamespace=urn://www.comac.com/mom");
		soapResponse.writeTo(System.out);
		// 拿到结果
		SOAPBody soapBodyRes = soapResponse.getSOAPBody();
		return null;
	}

	@Override
	public ResultBean clickPrintQar(QualityQarCheckInvoiceEntity qualityQarCheckInvoiceEntity) throws SOAPException, IOException {
		ResultBean resultBean = new ResultBean(true);
		// QAR编号
		String qarNo = qualityQarCheckInvoiceEntity.getQarNo();
		// 件号
		String partNo = qualityQarCheckInvoiceEntity.getPartNo();
		// 序列号
		String serialNumber = qualityQarCheckInvoiceEntity.getSerialNumber();
		// 批次号
		String stoveNo = qualityQarCheckInvoiceEntity.getStoveNo();
		String myNamespace = "mom";
		String myNamespaceURI = "urn://www.comac.com/mom";

		// Create SOAP Connection
		SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();

		SOAPConnection soapConnection = soapConnectionFactory.createConnection();
		// Create SOAP Message
		MessageFactory messageFactory = MessageFactory.newInstance("SOAP 1.1 Protocol");
		SOAPMessage soapMessage = messageFactory.createMessage();
		MimeHeaders hd = soapMessage.getMimeHeaders();
		hd.addHeader("Authorization", "Basic UElURVNUOmNvbWFjMTIz");
		SOAPPart soapPart = soapMessage.getSOAPPart();

		// SOAP Envelope
		SOAPEnvelope envelope = soapPart.getEnvelope();
		envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);

		// SOAP Body
		SOAPBody soapBody = envelope.getBody();
		SOAPElement soapBodyElem = soapBody.addChildElement("MT_MOM_QM_Request_Out", myNamespace);

		SOAPElement requestElem = soapBodyElem.addChildElement("QualityInspectionResult");
		SOAPElement brNoEle = requestElem.addChildElement("qarNo");
		brNoEle.addTextNode(qarNo);
		SOAPElement contentEle = requestElem.addChildElement("partCde");
		contentEle.addTextNode(partNo);
		SOAPElement reqSerialEle = requestElem.addChildElement("serialNum");
		reqSerialEle.addTextNode(serialNumber);
		SOAPElement batch = requestElem.addChildElement("batch");
		batch.addTextNode(stoveNo);
		soapMessage.saveChanges();
		soapMessage.writeTo(System.out);
		// Send SOAP Message to SOAP Serve
		SOAPMessage soapResponse = soapConnection.call(soapMessage,
				"http://10.14.3.83/XISOAPAdapter/MessageServlet?senderParty=&senderService=BC_MOM&receiverParty=&receiverService=&interface=SI_MOM_QM_Out&interfaceNamespace=urn://www.comac.com/mom");

		soapResponse.writeTo(System.out);
		// 拿到结果
		SOAPBody soapBodyRes = soapResponse.getSOAPBody();
		return resultBean;
	}

}
